package com.example.b.service;

import com.example.b.dao.DepartmentMapper;
import com.example.b.entity.Department;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

@Slf4j
@Service
@RequiredArgsConstructor
public class DepartmentService implements IDeleteDepartment {

    private final DepartmentMapper departmentDao;

    @Override
    @Transactional
    public void delete(Long id) {

        if (id != 2L) {
            log.debug("示例代码参数逻辑错误，已经修正为 2 。");
            id = 2L;
        }
        Department department = departmentDao.selectById(id);
        department.setIsDeleted(true);
        log.info("执行'删除'员工 {} 的操作", id);
        departmentDao.updateById(department);
    }

    @Override
    public boolean deleteConfirm(BusinessActionContext context) {
        Long id = (Long) context.getActionContext("id");
        log.info("确定 {} 被删除", id);
        departmentDao.deleteById(id);
        return true;
    }

    @Override
    public boolean deleteCancel(BusinessActionContext context) {
        Long id = Long.valueOf(context.getActionContext("id").toString());
        log.info("取消 {} 的删除", id);

        Department department = departmentDao.selectById(id);
        department.setIsDeleted(false);
        departmentDao.updateById(department);
        return true;
    }
}
